package com.itheima.demo;

import com.itheima.domain.UserDemo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DemoTest02 {

    //查询所有用户
    @Test
    public void test1() throws IOException {
        //查询所有用户
        //读取核心配置文件
        InputStream is = Resources.getResourceAsStream("configMybatis.xml");
        //构建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //获取SqlSession对象
        SqlSession session = factory.openSession();
        //调用statement对象并执行
        List<UserDemo> list = session.selectList("findAll");
        //事务提交和关流
        session.commit();
        is.close();
        System.out.println(list);
    }

    @Test
    public void test2() throws IOException {
        //根据用户id查询
        //读取核心配置文件
        InputStream is1 = Resources.getResourceAsStream("configMybatis.xml");
        //构建SqlSessionFactory对象
        SqlSessionFactory factory1 = new SqlSessionFactoryBuilder().build(is1);
        //获取SqlSessionFactory对象
        SqlSession session1 = factory1.openSession();
        //调用并执行session对象
        UserDemo user = session1.selectOne("one.findByID", 3);
        session1.commit();
        is1.close();
        System.out.println(user);
    }


    //统计人员数量
    @Test
    public void test3() throws IOException {
        //读取核心配置文件
        InputStream is = Resources.getResourceAsStream("configMybatis.xml");
        //创建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //获取SqlSessionFactory对象
        SqlSession session = factory.openSession();
        //执行session
        Integer i = session.selectOne("one.findCount");
        session.commit();
        is.close();
        System.out.println(i);
    }

    //根据姓名和年龄查找
    @Test
    public void test4() throws IOException {
        InputStream is = Resources.getResourceAsStream("configMybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession();
        //创建map集合,封装数据
        Map map = new HashMap();
        map.put("na", "李四");
        map.put("ae", 24);
        UserDemo user = session.selectOne("one.findNameAndAge", map);
        session.commit();
        is.close();
        System.out.println(user);

    }

    //模糊查询
    @Test
    public void test5() throws IOException {
        InputStream is = Resources.getResourceAsStream("configMybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession();
        //根据name属性"张"来查询
        List<UserDemo> list = session.selectList("findLikeName", "%张%");
        session.commit();
        is.close();
        System.out.println(list);
    }

    @Test
    public void test6() throws IOException {
        //读取核心配置
        InputStream is = Resources.getResourceAsStream("configMybatis.xml");
        //创建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //获取SqlSessionFactory对象
        SqlSession session = factory.openSession();
        //封装
        UserDemo user = new UserDemo();
        user.setName("周天宝");
        user.setAge(23);
        //执行session
        session.insert("one.findAdd", user);
        session.commit();
        is.close();
        System.out.println("user = " + user);
    }

    @Test
    //修改用户
    public void test7() throws IOException {
        InputStream is = Resources.getResourceAsStream("configMybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession();
        UserDemo user = new UserDemo(1,"周七",25);
        int update = session.update("one.findUpdate", user);
        session.commit();
        is.close();
    }

    @Test
    //删除用户
    public void text8() throws IOException {
        InputStream is = Resources.getResourceAsStream("configMybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession();
        int delete = session.delete("one.findDelete", 7);
        session.commit();
        is.close();
    }
}
